<?php
include_once('Smarty.class.php');
$main_smarty = new Smarty;

include('config.php');
include(mnminclude.'html1.php');
include(mnminclude.'ts.php');
include_once(mnminclude.'utils.php');
include(mnminclude.'user.php');
include_once(mnminclude.'KMessaging.php');
include(mnminclude.'smartyvariables.php');

// html tags allowed during submit
$main_smarty->assign('Story_Content_Tags_To_Allow', htmlspecialchars(Story_Content_Tags_To_Allow));

// breadcrumbs and page titles

$navwhere['text1'] = $main_smarty->get_config_vars('PLIGG_Visual_Breadcrumb_Submit');
$navwhere['link1'] = getmyurl('submit', '');
$main_smarty->assign('navbar_where', $navwhere);
$main_smarty->assign('posttitle', " / " . '信箱');

$main_smarty = do_sidebar($main_smarty);

// make sure user is logged in
force_authentication();

// module system hook
$vars = '';
check_actions('submit_post_authentication', $vars);//this line is no use here

define('pagename', 'communicate'); 
$main_smarty->assign('pagename', pagename);

if (isset($_REQUEST['view'])) {
	$view = $_REQUEST['view'];
}
else {
	$view = 'inbox';
}

switch ($view) {
	case 'inbox':
		show_inbox();
		break;
	case 'outbox':
		show_outbox();
		break;
	case 'editnewmsg':
		edit_newmsg();
		break;
	case 'contact':
		show_contact();
		break;
}

exit;

// prepare the submit thing form
function show_inbox() {
	global $main_smarty, $db, $dblang, $current_user, $the_template, $top_users_size, $offset, $rows;
	
	if ($current_user->user_id > 0) {
		$offset=(get_current_page()-1)*$top_users_size;
		
		$message = new KMessaging(true);
		$array = $message->GetAllMesseges(5, $current_user->user_id, '', 0, 'inbox');
		if(is_array($array)){
			$message_count = count($array);
			$main_smarty->assign('messages', $message_count);
	
			$i = 1;
			foreach($array as $key => $val){
				if($i == 1){$msg_first = $array[$key]['id'];}
					if($i > 1){$array[$key]['prev_message_id'] = 'my_message' . ($i - 1);}
					if($i < $message_count){$array[$key]['next_message_id'] = 'my_message' . ($i + 1);}
					$array[$key]['count'] = $i;
					$array[$key]['my_message_id'] = 'my_message' . $i;
					
					$user =  new User();
					$user->id = $array[$key]['sender'];
					if(!$user->read()) {
						echo "error 2";
						die;
					}
					$array[$key]['sender_name'] = $user->username;
					$array[$key]['sender_names'] = $user->names;
					$array[$key]['sender_view_url'] = my_base_url . my_pligg_base . '/user.php?login=' . $user->username;
					$array[$key]['Avatar_ImgSrc'] = get_avatar("small",'',$user->username,'',$user->id);
					//$array[$key]['body'] = save_text_to_html($array[$key]['body']); //cancel this line to use html tags
					$array[$key]['body'] = $array[$key]['body'];
					$user = "";
					
					$i = $i + 1;
				}
				$main_smarty->assign('msg_new_count', $i - 1);
				$main_smarty->assign('msg_array', $array);
			} else {
				$main_smarty->assign('msg_array', '');
			}
		}

	if ($current_user->user_level == 'god') {
		$main_smarty->assign('senderlevel',0);
	}
	if ($current_user->user_level == 'admin') {
		$main_smarty->assign('senderlevel',1);
	}
	if ($current_user->user_level == 'normal') {
		$main_smarty->assign('senderlevel',2);
	}
	$main_smarty->assign('doing','view_inbox');
	$main_smarty->assign('tpl_center', $the_template . '/communicate_inbox');

	$main_smarty->display($the_template . '/pligg.tpl');
}

// submit thing insert into table, and add owner if it already has one
function show_outbox() {
	global $main_smarty, $db, $dblang, $current_user, $the_template, $top_users_size, $offset, $rows;

	if ($current_user->user_id > 0) {
		$offset=(get_current_page()-1)*$top_users_size;
		
		$message = new KMessaging(true);
		$array = $message->GetAllMesseges(5, '', $current_user->user_id, 0, 'outbox');
		if(is_array($array)){
			$message_count = count($array);
			$main_smarty->assign('messages', $message_count);
	
			$i = 1;
			foreach($array as $key => $val){
				if($i == 1){$msg_first = $array[$key]['id'];}
					if($i > 1){$array[$key]['prev_message_id'] = 'my_message' . ($i - 1);}
					if($i < $message_count){$array[$key]['next_message_id'] = 'my_message' . ($i + 1);}
					$array[$key]['count'] = $i;
					$array[$key]['my_message_id'] = 'my_message' . $i;
					
					$user =  new User();
					$user->id = $array[$key]['receiver'];
					if(!$user->read()) {
						echo "error 2";
						die;
					}
					$array[$key]['receiver_name'] = $user->username;
					$array[$key]['receiver_names'] = $user->names;
					$array[$key]['receiver_view_url'] = my_base_url . my_pligg_base . '/user.php?login=' . $user->username;
					$array[$key]['Avatar_ImgSrc'] = get_avatar("small",'',$user->username,'',$user->id);
					//$array[$key]['body'] = save_text_to_html($array[$key]['body']); //cancel this line to use html tags
					$array[$key]['body'] = $array[$key]['body'];
					$user = "";
					
					$i = $i + 1;
				}
				$main_smarty->assign('msg_new_count', $i - 1);
				$main_smarty->assign('msg_array', $array);
			} else {
				$main_smarty->assign('msg_array', '');
			}
		}

	if ($current_user->user_level == 'god') {
		$main_smarty->assign('senderlevel',0);
	}
	if ($current_user->user_level == 'admin') {
		$main_smarty->assign('senderlevel',1);
	}
	if ($current_user->user_level == 'normal') {
		$main_smarty->assign('senderlevel',2);
	}
	$main_smarty->assign('doing','view_outbox');
	$main_smarty->assign('tpl_center', $the_template . '/communicate_outbox');

	$main_smarty->display($the_template . '/pligg.tpl');
}

function show_contact() {
	
}

function edit_newmsg() {
	global $main_smarty, $db, $dblang, $current_user, $the_template, $top_users_size, $reco_offset, $reco_rows, $offset, $rows;
	
	//get reco_user (contact or friend here)
	if (!isset($_POST['norecouser'])) {
		// first time load this page
		if (isset($_REQUEST['receiver'])) {
			$receiver = $_REQUEST['receiver'];
			$main_smarty->assign("receiver",$receiver);
		}
		if (isset($_REQUEST['receiver_names'])) {
			$receiver_names = $_REQUEST['receiver_names'];
			$main_smarty->assign("receiver_names",$receiver_names);
		}
		$noresult = 'true'; //no search result
		$main_smarty->assign('noresult',$noresult);
	
		$main_smarty->assign('reco_cur_page',1);
		$reco_offset = 0;
		$reco_sqlquery = "SELECT COUNT(DISTINCT friend_to) FROM " . table_friends . " WHERE friend_from = " . $current_user->user_id;
		$reco_rows = $db->get_var($reco_sqlquery);
		if ($reco_rows > 0) {
			$main_smarty->assign('norecouser', 'false');
			$reco_sqlquery = "SELECT DISTINCT " . table_users . ".user_id," . table_users . ".user_login," . table_users . ".user_names" ." FROM " . table_friends . ", " . table_users . " WHERE " . table_friends. ".friend_from=" .$current_user->user_id . " AND " . table_friends . ".friend_to=" . table_users . ".user_id"  . " ORDER BY " . table_friends . ".friend_id DESC LIMIT $reco_offset,$top_users_size";
			$reco_searchsql = mysql_query($reco_sqlquery);
			$reco_userlist = array();
			while ($row = mysql_fetch_array ($reco_searchsql, MYSQL_ASSOC)) array_push ($reco_userlist, $row);
			foreach($reco_userlist as $key => $val){
				$reco_userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			}
			$main_smarty->assign('reco_userlist', $reco_userlist);
		}
		else {
			$main_smarty->assign('norecouser', 'true');
		}
	}
	else {
		//come back from this page
		$main_smarty->assign('norecouser', $_POST['norecouser']);
		if ($_POST['norecouser'] == 'false') {//get contact at the indicated page
			$main_smarty->assign('reco_cur_page',reco_get_current_page());
			$reco_offset=(reco_get_current_page()-1)*$top_users_size;
			$reco_sqlquery = "SELECT COUNT(DISTINCT friend_to) FROM " . table_friends . " WHERE friend_from = " . $current_user->user_id;
			$reco_rows = $db->get_var($reco_sqlquery);
			$reco_sqlquery = "SELECT DISTINCT " . table_users . ".user_id," . table_users . ".user_login," . table_users . ".user_names" ." FROM " . table_friends . ", " . table_users . " WHERE " . table_friends. ".friend_from=" .$current_user->user_id . " AND " . table_friends . ".friend_to=" . table_users . ".user_id"  . " ORDER BY " . table_friends . ".friend_id DESC LIMIT $reco_offset,$top_users_size";
			$reco_searchsql = mysql_query($reco_sqlquery);
			$reco_userlist = array();
			while ($row = mysql_fetch_array ($reco_searchsql, MYSQL_ASSOC)) array_push ($reco_userlist, $row);
			foreach($reco_userlist as $key => $val){
				$reco_userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			}
			$main_smarty->assign('reco_userlist', $reco_userlist);	
		}
		//deal the search
		$main_smarty->assign('usernames',$_POST['usernames']);
		$main_smarty->assign('cur_page',get_current_page());
		if (trim($_POST['usernames']) != '') {
			$offset=(get_current_page()-1)*$top_users_size;
			$sqlquery = "SELECT count(*) FROM " . table_users . " where user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%'";
			$rows = $db->get_var($sqlquery);
			if (!($offset<$rows)) {
				$offset = 0;//in case the user change the keyword to search
				$main_smarty->assign('cur_page',1);
				$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
			}
			if ($rows>0) {
				$sqlquery = "SELECT * FROM " . table_users . " where user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%'";
				$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
				$searchsql = mysql_query($sqlquery);
				$userlist = array();
				while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
				foreach($userlist as $key => $val){
					$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
				}
				$main_smarty->assign('userlist', $userlist);
				$main_smarty->assign('rows', $rows);
			}	
			else {
				//no such user in the system
				$noresult = 'true';
				$main_smarty->assign('noresult',$noresult);
				$main_smarty->assign('nouserhint','系统中没有此用户');
			}
		}
		if (trim($_POST['usernames']) == '') {
			$noresult = 'true';
			$main_smarty->assign('noresult',$noresult);	
		}
	}
	
	$main_smarty->assign('doing','edit_newmsg');
	$main_smarty->assign('sender', $current_user->user_id);
	if ($current_user->user_level == 'god') {
		$main_smarty->assign('senderlevel',0);
	}
	if ($current_user->user_level == 'admin') {
		$main_smarty->assign('senderlevel',1);
	}
	if ($current_user->user_level == 'normal') {
		$main_smarty->assign('senderlevel',2);
	}
	$main_smarty->assign('tpl_center', $the_template . '/communicate_editnewmsg');
	$main_smarty->display($the_template . '/pligg.tpl');
	
}

?>
